//
//  YCEditorPasswordViewController.m
//  YCSchool
//
//  Created by Yanglixia on 16/5/10.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//

#import "YCEditorPasswordViewController.h"

@interface YCTableViewTextFieldCell : UITableViewCell

@property (nonatomic,readonly) UITextField *textField;

@end

@implementation YCTableViewTextFieldCell

@synthesize textField = _textField;

- (UITextField *)textField
{
    if(!_textField)
    {
        _textField = [[UITextField alloc] init];
    }
    return _textField;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if(!self.textField.window)
    {
        [self.contentView addSubview:self.textField];
    }
    
    CGRect leftRect,rightRect;
    CGRectDivide(self.contentView.bounds, &leftRect, &rightRect, 107, CGRectMinXEdge);
    
    self.textLabel.frame = leftRect;
    self.textField.frame = rightRect;
}

@end


@interface YCEditorPasswordViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (nonatomic,readonly) UITableView *tableView;
@property (nonatomic,readonly) UIView *footerView;
@property (nonatomic,readonly) NSArray *items;

@end

@implementation YCEditorPasswordViewController
@synthesize tableView = _tableView;
@synthesize footerView = _footerView;

#pragma mark - 属性获取方法

- (UITableView *)tableView
{
    if(!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor yc_colorWithUInt:0xefeff4];
        
        
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _tableView.bounds.size.width, 23.5)];
        _tableView.tableFooterView = self.footerView;
    }
    return _tableView;
}

- (UIView *)footerView
{
    if(!_footerView)
    {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 105)];
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectInset(_footerView.bounds, 18, 33)];
        button.backgroundColor = [UIColor yc_colorWithUInt:0x00cc00];
        button.layer.cornerRadius = button.bounds.size.height / 2.0;
        button.layer.masksToBounds = YES;
        [button setTitle:@"确认修改" forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        [button addTarget:self action:@selector(didTouchUpInsidePostButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [_footerView addSubview:button];
    }
    return _footerView;
}

#pragma mark - 视图控制器生命周期

- (void)dealloc
{
    
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
//    [self.tableView yc_clearSeparatorInset];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = NSLocalizedString(@"密码设置", nil);
    [self yc_addBackBarButtonItem];
    
    [self layout];
}

#pragma mark - 自定义的方法

- (void)createData
{
    _items = @[@{@"t":@"原密码",@"h":@"请输入旧密码"},
               @{@"t":@"新密码",@"h":@"请输入密码"},
               @{@"t":@"确认密码",@"h":@"请再次输入密码"}];
}

- (void)layout
{
    [self createData];
    
    [self.view addSubview:self.tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.and.top.bottom.equalTo(self.view);
    }];
}

#pragma mark - 自定义方法

- (void)didTouchUpInsidePostButton:(UIButton *)postButton
{
    NSString *password = [[(YCTableViewTextFieldCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] textField].text yc_md5String];
    NSString *newPassword = [[(YCTableViewTextFieldCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]] textField].text yc_md5String];
    NSString *confirmPassword = [[(YCTableViewTextFieldCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]] textField].text yc_md5String];
    
    if(password.length < 1)
    {
        [SVProgressHUD showErrorWithStatus:@"请输入原密码"];
        return;
    }
    
    if(newPassword.length < 1)
    {
        [SVProgressHUD showErrorWithStatus:@"请输入新密码"];
        return;
    }
    
    if(newPassword.length < 6)
    {
        [SVProgressHUD showErrorWithStatus:@"新密码至少为6位"];
        return;
    }
    
    if(![newPassword isEqualToString:confirmPassword])
    {
        [SVProgressHUD showErrorWithStatus:@"两次输入的密码不一致"];
        return;
    }
    
//    @yc_weakify(self);
    
    NSDictionary *ps = @{@"srcPwd":password,@"newPwd":newPassword};
    
//    GOWebServiceSuccessCallBack success = ^(AFHTTPRequestOperation *operation, GOWebServiceResponse *response) {
//        if(response.success)
//        {
//            @go_strongify(self);
//            
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [SVProgressHUD showSuccessWithStatus:@"设置密码成功"];
//                [self.navigationController popViewControllerAnimated:YES];
//            });
//        }
//        else
//        {
//            dispatch_async(dispatch_get_main_queue(), ^{
//                NSString *message = @"修改密码失败";
//                [SVProgressHUD showErrorWithStatus:message];
//            });
//        }
//    };
    
//    GOWebServiceFailureCallBack failure = ^(AFHTTPRequestOperation *operation, NSError *error) {
//        
//    };
//    
//    [SVProgressHUD showWithStatus:@"登录中..."];
//    
//    [[GOWebService service] postAction:GOAPIUserUserinfoChangePwd
//                                params:ps
//                               success:success
//                               failure:failure];
    
}

#pragma mark - UITableView delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CELLID = @"CELLID";
    
    YCTableViewTextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID];
    NSDictionary *titleData = self.items[indexPath.row];
    
    if(!cell)
    {
        cell = [[YCTableViewTextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELLID];
        cell.textField.secureTextEntry = YES;
        cell.textField.delegate = self;
        cell.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textField.returnKeyType = UIReturnKeyDone;
    }
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.firstLineHeadIndent = 23;
    
    cell.textLabel.attributedText = [[NSAttributedString alloc] initWithString:titleData[@"t"] attributes:@{NSParagraphStyleAttributeName:style}];
    cell.textField.placeholder = titleData[@"h"];
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [cell go_clearSeparatorInset];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    return YES;
}



@end
